<?php
include_once "BaseM.php";
 
class DogM extends BaseM{
	
	private $table = "dog";
	
	public function __construct() {
		parent::__construct ();
	}
	
	public function insert($obj) {
		$sql = $this->genInserSQL($this->table, $obj);
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return mysql_insert_id( );
	}
	
	public function update($obj) {
		$sql = $this->genUpdateSQL($this->table, $obj, array("id"));
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return  $result;
	}
	
	public function delete($key) {
		$sql="delete from $this->table where id = $key";
		$count = @mysql_query( $sql ) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $count;
	}
	
	public function searchBlood($blood, $searchStart, $searchEnd, $county, $liveZone){
	
		$whereStr = " 1 = 1 ";
		$whereBlood = "";
		$whereStr .= $county != "" ? " and member_id in (select id from member where county = (select county from zipcode_tw where zip_code in ( $county ) ) ) " : "" ;
		$whereStr .= $liveZone != 0 ? " and member_id in (select id from member where live_zone in ( $liveZone )) " : "" ;
		
		$whereBlood .= $blood['blood_dea10'] == 0 ? " or blood_dea10 = 1 " : "";
		$whereBlood .= $blood['blood_dea11'] == 0 ? " or blood_dea11 = 1 " : "";
		$whereBlood .= $blood['blood_dea12'] == 0 ? " or blood_dea12 = 1 " : "";
		$whereBlood .= $blood['blood_dea3'] == 0 ? " or blood_dea3 = 1 " : "";
		$whereBlood .= $blood['blood_dea4'] == 0 ? " or blood_dea4 = 1 " : "";
		$whereBlood .= $blood['blood_dea5'] == 0 ? " or blood_dea5 = 1 " : "";
		$whereBlood .= $blood['blood_dea6'] == 0 ? " or blood_dea6 = 1 " : "";
		$whereBlood .= $blood['blood_dea7'] == 0 ? " or blood_dea7 = 1 " : "";
		$whereBlood .= $blood['blood_dea8'] == 0 ? " or blood_dea8 = 1 " : "";
		
		$whereBlood = substr($whereBlood, 4);
		
		if($whereBlood != ""){
			$whereStr .= " and ( $whereBlood ) ";
		}
		
		$countSql = "select count(id) as count from $this->table where $whereStr ";
		$count = $this->querySQL($countSql, true);
		
		$resultSql="
			select id as dogId, name, (select county from member where id = dog.member_id )as county, photo,
				year( from_days( datediff( now( ), birthday ))) as old,
				blood_dea10, blood_dea11, blood_dea12, blood_dea3, blood_dea4, blood_dea5, blood_dea6, blood_dea7, blood_dea8
			from $this->table
			where $whereStr
			limit $searchStart, $searchEnd ";
		$result = $this->querySQL($resultSql);
		return array( "count" => $count->count, "result" => $result );
	}
	
	public function getList($memberId, $searchStart, $searchEnd){
	
		$whereStr = " member_id = $memberId ";
	
		$countSql = "select count(id) as count from $this->table where $whereStr ";
		$count = $this->querySQL($countSql, true);
	
		$resultSql = "
			select id as dogId, name, (select county from member where id = dog.member_id )as county, photo,
				year( from_days( datediff( now( ), birthday ))) as old,
				blood_dea10, blood_dea11, blood_dea12, blood_dea3, blood_dea4, blood_dea5, blood_dea6, blood_dea7, blood_dea8
			from $this->table
			where $whereStr
			limit $searchStart, $searchEnd ";
		$result = $this->querySQL($resultSql);
		return array( "count" => $count->count, "result" => $result );
	}
	
	public function getBloodDetail($id){
		$sql="
			select 
				p.id as id, p.name as name, p.weight as weight, p.photo as photo, m.gender,
				year( from_days( datediff( now( ), p.birthday ))) as old,
				p.blood_dea10, p.blood_dea11, p.blood_dea12, p.blood_dea3, p.blood_dea4, 
				p.blood_dea5, p.blood_dea6, p.blood_dea7, p.blood_dea8,  
				m.county as county, 
				m.realname as ownerRealName,
				m.nickname as ownerNickName,
				m.county as ownerCounty,
				m.account as account,
				m.phone as phone,
				case m.show_line when 1 then m.no_line when 0 then '' end no_line,
				case m.show_fb when 1 then m.no_fb when 0 then '' end no_fb,
				case m.show_wechat when 1 then m.no_wechat when 0 then '' end no_wechat,
				case m.show_skype when 1 then m.no_skype when 0 then '' end no_skype,
				case m.show_gplus when 1 then m.no_gplus when 0 then '' end no_gplus
			from $this->table as p inner join member as m
			where p.id = $id and p.member_id = m.id";
		return $this->querySQL($sql, true);
	}
	
	public function getMyPetDetail($memberId, $id){
		$sql="
			select
				p.id as id, p.name as name, p.weight as weight, p.photo as photo,
				year( from_days( datediff( now( ), p.birthday ))) as old,
				p.blood_dea10, p.blood_dea11, p.blood_dea12, p.blood_dea3, p.blood_dea4, 
				p.blood_dea5, p.blood_dea6, p.blood_dea7, p.blood_dea8,
				m.county as county
			from $this->table as p inner join member as m
			where p.id = $id and p.member_id = m.id and p.member_id = $memberId";
		return $this->querySQL($sql, true);
	}
	
	
}

?>